package com.ray.auth.system.service.impl;


import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.emun.UseStatus;
import com.ray.auth.system.service.AppService;
import com.ray.auth.system.table.entity.App;
import com.ray.auth.system.table.entity.AppExample;
import com.ray.auth.system.table.entity.AppExample.Criteria;
import com.ray.auth.system.table.mapper.AppMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class AppServiceImpl extends BaseService implements AppService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(AppServiceImpl.class);
	
	@Autowired
	private AppMapper appMapper;

	/**
	 * 新增
	 */
	@Transactional
	public Result<Object> addApp(App params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
	//	params.setAppId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=appMapper.insertSelective(params);
		if(count!=1){
			logger.error("AppServiceImpl=>addApp is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public App queryApp(String id, LoginUser user) {
		return appMapper.selectByPrimaryKey(id);
	}

	@Transactional
	public Result<Object> updateApp(App params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=appMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("AppServiceImpl=>updateApp is error");
			throw new BussinessException(-1, ErrorMsg.UPDATE_ERROR);
		}
		return result;
	}
	@Transactional
	public Result<Object> deleteApp(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		App params=new App();
		params.setAppId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=appMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("AppServiceImpl=>deleteApp is error");
			throw new BussinessException(-1, ErrorMsg.DELETE_ERROR);
		}
		return result;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<App> queryApp(App params, PageDto<App> page, LoginUser user) {
		AppExample example=new AppExample();
		Criteria criteria =  example.createCriteria();
		if(!user.isSuperManager()){
			criteria.andTenantIdEqualTo(user.getTenantId());
			criteria.andCreateByUserEqualTo(user.getUserId());
		}
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		page.setTotal(appMapper.countByExample(example));
		page.setRecords(appMapper.selectByExample(example));
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<App> queryMyTodoApp(App params, PageDto<App> page, LoginUser user) {
		AppExample example=new AppExample();
		Criteria criteria =  example.createCriteria();
		criteria.andTenantIdEqualTo(user.getTenantId());
		criteria.andCreateByUserEqualTo(user.getUserId());
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(appMapper.countByExample(example));
		page.setRecords(appMapper.selectByExample(example));
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<App> queryUseAppList() {
		App params = new App();
		params.setIsDelete(DeleteEnum.USE.getValue());
		params.setAppStatus(UseStatus.ON.getType());
		return appMapper.selectByRecord(params);
	}

}
